package br.edu.ufcg.greengrid.model.machine.state;

import br.edu.ufcg.greengrid.model.machine.Machine;

public abstract class GoingGreenMachineState extends AbstractMachineState {

	private final long waitTime;
	
	private long waitTimeCounter;
	
	public GoingGreenMachineState(long waitTime){
		
		this.waitTime = waitTime;
	}
	
	@Override
	public MSEnum getType() {

		return MSEnum.GOING_GREEN;
	}

	protected boolean isWaitTimeZero(){
		
		return this.waitTimeCounter == 0L;
	}
	
	protected void decCountdown() {
		super.decCountdown();
		this.waitTimeCounter--;
	}
	
	public void setCountdown(long countdown){
		
		this.waitTimeCounter = this.waitTime;
		super.setCountdown(countdown);
	}

	public void changeStateIfNeeded(Machine m) {
		MSEnum newStateType = null;
		long countdown = 0L;
		
		if(isCountdownZero()){
			newStateType = MSEnum.USER;
			countdown = 0L;
		} else if(isWaitTimeZero()){
			newStateType = MSEnum.GREEN;
			countdown = this.countdown;
		}
		
		if(newStateType != null){
			m.changeState(newStateType, countdown);
		}
	}
	
	
}
